using System;
using System.Collections.Generic;
using System.Text;

namespace GuavaData
{
    public class CrossJoin : Operator
    {
        // Relational algebra cross product operator: child1 X child2

        #region Fields
        Operator child1;
        public Operator Child1
        {
            get { return child1; }
            set { child1 = value; }
        }

        Operator child2;
        public Operator Child2
        {
            get { return child2; }
            set { child2 = value; }
        }
        #endregion Fields

        public CrossJoin(Operator child1, Operator child2)
        {
            this.child1 = child1;
            this.child2 = child2;
            child1.parent = this;
            child2.parent = this;

            // List of columns and domains is equal to the combination of the two from the base expressions
            this.exposedColumns = new List<string>(this.child1.exposedColumns);
            this.exposedColumns.AddRange(this.child2.exposedColumns);
            this.exposedDomains = new List<GuavaData.Domain.Domain>(this.child1.exposedDomains);
            this.exposedDomains.AddRange(this.child2.exposedDomains);
        }

        public override SqlText Visit(Provider qv)
        {
            return qv.Transform(this);
        }
        public override void Visit(Transform t, Command c)
        {
            t.Expand(this, c);
        }
        public override Operator Clone()
        {
            return new CrossJoin(this.child1.Clone(), this.child2.Clone());
        }

        public override void NotifyChange(Operator old, Operator newOp)
        {
            if (this.child1 == old)
            {
                this.child1 = newOp;
                newOp.parent = this;
            }
            if (this.child2 == old)
            {
                this.child2 = newOp;
                newOp.parent = this;
            }
        }


        public override void Visit(Channel c, string table)
        {
            c.MapAliases(this, table);
        }

    }
}
